<template>
  <el-dialog
    :title="id ? '编辑属性' : '添加属性'"
    :visible.sync="visible"
    width="500px"
    :before-close="
      () => {
        $emit('close');
      }
    "
  >
    <el-form
      ref="propForm"
      size="small"
      :rules="rules"
      :model="form"
      label-width="100px"
    >
      <el-form-item label="商品分类" prop="kind">
        <el-cascader
          v-model="form.kind"
          :options="kindList"
          :props="casProps"
          expand-trigger="hover"
          clearable
        />
      </el-form-item>
      <el-form-item label="属性名称" prop="spec_name">
        <el-input
          v-model="form.spec_name"
          placeholder="最多六个字"
          :maxlength="6"
        />
      </el-form-item>
      <el-form-item :label="'可选值' + (id ? '追加' : '')" prop="spec">
        <el-input
          v-model="form.spec"
          placeholder="每个可选值之间用加号“+”隔开,编辑时不能删除已有值"
          type="textarea"
          rows="4"
          resize="none"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('close')">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getPropDetail, addProp } from '@/api/good-mng'
export default {
  props: ['visible', 'kindList', 'id'],
  data() {
    return {
      form: {
        spec_name: '',
        kind: [],
        spec: ''
      },
      rules: {
        kind: [{ required: true, message: '请选择商品分类', trigger: 'blur' }],
        spec_name: [{ required: true, message: '属性名称', trigger: 'blur' }],
        spec: [{ required: true, message: '请输入可选值', trigger: 'blur' }]
      },
      casProps: {
        expandTrigger: 'hover',
        checkStrictly: true,
        value: 'id',
        label: 'kind_name',
        children: 'lower'
      }
    }
  },
  watch: {
    visible: function(val) {
      if (val) {
        if (this.id) {
          getPropDetail({ id: this.id }).then(res => {
            if (res.code === 200) {
              this.form.spec_name = res.data.data.spec_kind_name
              // this.form.spec = res.data.lower
              //   .map(item => {
              //     return item.spec_name;
              //   })
              //   .join(";");
              this.form.kind = [
                res.data.data.higherup,
                res.data.data.commodity_kind_id
              ]
            }
          })
        }
      } else {
        this.$refs['propForm'].resetFields()
      }
    }
  },
  created() {},
  methods: {
    submit() {
      this.$refs['propForm'].validate(valid => {
        if (valid) {
          const obj = { ...this.form }
          // obj.spec = obj.spec.replace(/，/g, ",");
          obj.kind = obj.kind[1]
          obj.id = this.id ? this.id : null
          addProp(obj).then(res => {
            if (res.code === 200) {
              this.$message({
                message: this.id ? '修改成功' : '添加成功',
                type: 'success'
              })
              this.$emit('close')
            }
          })
        }
      })
    }
  }
}
</script>

<style></style>
